#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define mod 1000000007
#define MOD 999999997
using namespace std;
inline int read(){
    int k=0,f=1;
	char ch=getchar();
    while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
    while(ch>='0'&&ch<='9'){
		k=k*10+ch-'0';
		ch=getchar();
	}
    return k*f;
}
const int N=2e3+10;
int n,m,add[N],jian[N],f[N][N];
char s[N];
struct node{
	char t;
	int x,y;
}a[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    freopen("hw.in","r",stdin);
    freopen("hw.out","w",stdout);
    cin>>n>>m;
    cin>>s+1;
    for(int i=1;i<=n;i++){
    	cin>>a[i].t>>a[i].x>>a[i].y;
    	add[a[i].t-'a']=a[i].x;
    	jian[a[i].t-'a']=a[i].y;
    }
    for(int i=1;i<=m;i++){
    	for(int j=1;j<=m;j++)f[i][j]=0x3f3f3f;
    	f[i][i]=0;
    	if(s[i]==s[i+1])f[i][i+1]=0;
    	else f[i][i+1]=min(min(add[s[i]-'a'],jian[s[i]-'a']),min(add[s[i+1]-'a'],jian[s[i+1]-'a']));
    }
    for(int c=3;c<=m;c++)
    	for(int l=1;l+c-1<=m;l++){
    		int r=l+c-1;
    		if(s[l]==s[r])f[l][r]=f[l+1][r-1];
    		else{
    			f[l][r]=min(min(f[l][r],f[l+1][r]+add[s[l]-'a']),min(f[l][r],f[l+1][r]+jian[s[l]-'a']));
    			f[l][r]=min(min(f[l][r],f[l][r-1]+add[s[r]-'a']),min(f[l][r],f[l][r-1]+jian[s[r]-'a']));
    		}
    	}
    cout<<f[1][m];
	return 0;
}

